package org.example;

import com.example.MultiParamCallback;
import com.example.MultiParams;
import com.example.ParametrizedCallback;
import com.example.utils.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.locks.ReentrantLock;
import java.io.FileWriter;
import java.util.List;
import java.util.Arrays;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {


    //主程序入口
    public static void main(String[] args) {
        //TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
        //runLine();
        //runWithManyParams();
        queryMysql();
    }

    //  测试查询数据
    public static void queryMysql() {
        // 测试查询操作
        testSelect();
        // 测试插入操作
        testInsert();
        // 测试更新操作
        testUpdate();
        // 测试删除操作
        //testDelete();
    }

    // 查询数据库
    public static void testSelect() {
        String sql = "SELECT * FROM users order by id desc";
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> result = PersonalTool.queryDataFromMysql(sql, params);
        System.out.println("查询操作结果:");
        printResult(result);
    }

    // 写入数据
    public static void testInsert() {
        String sql = "INSERT INTO users (username, password) VALUES (?, ?)";
        Map<String, Object> params = new HashMap<>();
        params.put("param1", "testuser");
        params.put("param2", "testpassword");
        Map<String, Object> result = PersonalTool.queryDataFromMysql(sql, params);
        System.out.println("插入操作结果:");
        printResult(result);
    }

    // 更新数据
    public static void testUpdate() {
        String sql = "UPDATE users SET password = ? WHERE username = ?";
        Map<String, Object> params = new HashMap<>();
        params.put("param1", "newpassword");
        params.put("param2", "testuser");
        Map<String, Object> result = PersonalTool.queryDataFromMysql(sql, params);
        System.out.println("更新操作结果:");
        printResult(result);
    }

    // 删除数据
    public static void testDelete() {
        String sql = "DELETE FROM users WHERE username = ?";
        Map<String, Object> params = new HashMap<>();
        params.put("param1", "testuser");
        Map<String, Object> result = PersonalTool.queryDataFromMysql(sql, params);
        System.out.println("删除操作结果:");
        printResult(result);
    }

    // 打印数据库结果
    public static void printResult(Map<String, Object> result) {
        System.out.println("操作状态: " +  result.get("status"));
        if (result.containsKey("message")) {
            System.out.println("错误信息: " + result.get("message"));
        }
        if (result.containsKey("data")) {
            List<Map<String, Object>> data = (List<Map<String, Object>>) result.get("data");
            System.out.println("查询结果:");
            for (Map<String, Object> row : data) {
                for (Map.Entry<String, Object> entry : row.entrySet()) {
                    System.out.println(entry.getKey() + ": " + entry.getValue());
                }
                System.out.println("---------------------");
            }
        }
        if (result.containsKey("rows")) {
            System.out.println("受影响的行数: " + result.get("rows"));
        }
        System.out.println();
    }

    // aes 加密，解密测试
    public static void aesDemo() {

        String input ="{\"name\":\"tom\",\"age\":\"12\"}";
        String key = "h2Gix9H7XRi8C4Ie"; // 128 位密钥（16 字节）
        String iv = "6MXqZeseGLSng7Tt"; // 初始化向量（16 字节）

        // ECB 模式加密和解密
        String encryptedECB = PersonalTool.aesEncode(input, key, PersonalTool.ECB_MODE);
        System.out.println("ECB 模式加密后的结果: " + encryptedECB);
        String decryptedECB = PersonalTool.aesDecode(encryptedECB, key, PersonalTool.ECB_MODE);
        System.out.println("ECB 模式解密后的结果: " + decryptedECB);

        // CBC 模式使用 iv 加密和解密
        String encryptedCBCWithIv = PersonalTool.aesEncode(input, key, PersonalTool.CBC_MODE, iv);
        System.out.println("CBC 模式使用 IV 加密后的结果: " + encryptedCBCWithIv);
        String decryptedCBCWithIv = PersonalTool.aesDecode(encryptedCBCWithIv, key, PersonalTool.CBC_MODE, iv);
        System.out.println("CBC 模式使用 IV 解密后的结果: " + decryptedCBCWithIv);

        // CBC 模式不使用 iv 加密和解密（使用默认 iv）
        String encryptedCBCWithoutIv = PersonalTool.aesEncode(input, key, PersonalTool.CBC_MODE);
        System.out.println("CBC 模式不使用 IV 加密后的结果: " + encryptedCBCWithoutIv);
        String decryptedCBCWithoutIv = PersonalTool.aesDecode(encryptedCBCWithoutIv, key, PersonalTool.CBC_MODE);
        System.out.println("CBC 模式不使用 IV 解密后的结果: " + decryptedCBCWithoutIv);
    }

    // 测试rsa加密
    public static void rsaDemo() {
        // 经过测试后，发现可以正常加密解密
        String publicKeyString = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4pMNiiH5+F86Piky+R8s48BeybSYT05B1DMF76y2pqzHlqBRUDIkUJ1lnd6L8yETvG2zdpO0HJGY4bKxQ94WzuJhUoXxTEOhaUdq8xFxmB5u7EKPDAxVmNKWgGmjLZJBj/ws/EE9Pe8hsGT0saxYOFp+WIEzvhMbEllLRPV0sToSD+lvIoN740rabCOUVhhpBDy5Qhk/Jw2FbUgebhwLx32O5dtARAGrMkEiYILghFCRJGBN/4p5JVSI7lDyUkE62D+xtMzMosvK9r/NasoHYvLQ5SAUJn+CWL/ZN+oMNXUHJQldwJZIZqc2dCJke8GimRh6Q7N1Vrkf4CUK425QuwIDAQAB";
        String privateKeyString = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDikw2KIfn4Xzo+KTL5HyzjwF7JtJhPTkHUMwXvrLamrMeWoFFQMiRQnWWd3ovzIRO8bbN2k7QckZjhsrFD3hbO4mFShfFMQ6FpR2rzEXGYHm7sQo8MDFWY0paAaaMtkkGP/Cz8QT097yGwZPSxrFg4Wn5YgTO+ExsSWUtE9XSxOhIP6W8ig3vjStpsI5RWGGkEPLlCGT8nDYVtSB5uHAvHfY7l20BEAasyQSJgguCEUJEkYE3/inklVIjuUPJSQTrYP7G0zMyiy8r2v81qygdi8tDlIBQmf4JYv9k36gw1dQclCV3AlkhmpzZ0ImR7waKZGHpDs3VWuR/gJQrjblC7AgMBAAECggEASu7ccvsyq0h/QDCq+YYsd/RWxPnTx6K3lcD8LRyfWbdZ2QCTc+Tparnrcg84lMOzPcXALS7DShGDEN3iz3HDdE+dZUgEGjdhyipC/jmeKnOiNsP6gD8SDhVTjguh2n/86+w4SoMfCH0uVKLG5H39vUF8eLPeU3YJJYPfp2JQYg4VpqBzmPSwhxH9tcg8gcFxuJU8To0qN4IpL0TwKfwlGQAppDktveOhzOTuqEcpDqoPhkMcPtbayniNiM+polkME5aw9FPq51Z+IILeSVQAb8iwSe5CbCqsPRPqgfz8A2/sbezr5ymZS/FjGs5HDvPqylQNisj8ZzvqGgf+CPL6IQKBgQDouxCrpXR5kI7NW3wUi0q5WXN8Rup3IgiroO4ADPTtyqRk3s55mtkUEoXZmQELyMZZdr9LDLtwPxMj2CgmamIZJj7HVpIb7jgCvRPbCn4D0LZI6eCa248GeHVtO9oNimkhaV/AVw06hhcKg9XAAkxCm9cxss5MehyVpGL5RbBBWwKBgQD5OmmJJIfrbh/T+Qnh4jOs6jHPKHYC0DN6kOlAoHHl8rkc768JmvmS7kIK54Fv8Efy8g+a1AQWuHKoseqF5rG7CBFowjM2CbyUAJy7BWOoG8aAeVxM8V/ksmKOi4m/6STnMYoLuiDkeQxjeOV0ztdhdnfq1DldXxLD6GHfVkLsIQKBgQDb+LPxCRnmLPRMtcO5x06BkkLNnFnQ8KIxVpKeCaEXgQmRLecKmYnEpx1MZonOkZ6owMxeK1Ke7X8xygVpCfRewP9lejXvoGHgdMZnqqshAAzLLgPVWuD8xK546LFZSxHDOs3+BknCnyr4DVPjgEiETLhBiTfkoSaTaiUCZxQJMQKBgGlsRfwXEc+Ks7fsnjuIfadlIVCpotGp1DzCU4hc1hEYzSjqUjZT8cIvOox2RRBeqXCg5+zsRhZ+SzIEoFmnrgKZdRPFIfh3wecxIxsROoh/x0XNEDgT2iZZNstpRwmAy3VoRSaUwxkiAxWIrDbuz7iQbkVNJD7U2pE4GT5ptMFBAoGAfcrHlzYYuxEUEEqUux72Cym2TF4h7BBXozLBeiE98lF9XuAAO0CdEt93WW7cfGmZ6NZOC/RyU/PpD7uZG0DmxUWy/3m0PSXfuQOLih7sAWhjjDxBvaOjXQPCj6cCfIZQHlHqRoeEqj6HEpeMOScH2zHWrTfy0HemFkppTZ/0xt4=";
        // 更换秘钥
        publicKeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDDHc+PP8LuTlBL1zCX+lh9kcurgHHIXFnV/tDK789DaJuhwZvQ1lu5Zdcn+ULbNUKkB6b5tCP0sZxlpoCVKMyKHtdeh/YGXwBD8sMc+XcRs0eh3/tyr4EoBu3bomzHWDGmHjH/F5GotFTrGcB6xQwAROy4mT5SketlQ3c7tucI+QIDAQAB";
        privateKeyString = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMMdz48/wu5OUEvXMJf6WH2Ry6uAcchcWdX+0Mrvz0Nom6HBm9DWW7ll1yf5Qts1QqQHpvm0I/SxnGWmgJUozIoe116H9gZfAEPywxz5dxGzR6Hf+3KvgSgG7duibMdYMaYeMf8Xkai0VOsZ" +
                "wHrFDABE7LiZPlKR62VDdzu25wj5AgMBAAECgYBKcdxYrp5EaHLwjNlIk0ciGfeYpvhC1yGbqY6mb1soQAhpbkJyKudyVG4EHXGpy6dyiEzoJxg063NdwWp7/sYTHk/N13UzGTudIKuNacnJk0WKu4owQticC71ZIqUjSZgN0CiEKQ6YfoGOFTzeMqzVYQjI" +
                "mPzGdLK74y3YYlmigQJBAObzhzYlWjOypx3klmrPTu2KXPg3ATTEB8kN/isY8bYuikVdd2yUd0AvaC7PPwEEjGmsSrEeXw1tsVfZ8VkBaikCQQDYR0+8VzGLdgIFQc/6+IY5fQlEt/Hc7qsi7JT4o+f+BGJlAT7+OeDMThavKdWq1UvZDyCKdtYRfxQ1jj7D" +
                "4yJRAkBrG6InkGcm9sHecTb5Ti+ypqq7Svc6O3fI3L51ylm/PhJOXSyXpLsxf0r3+pGjrTJZh9gUEJvQpIDM13zA5JERAkBI2zTsED9baIRjuvjR5Xhp00oVARYTw76YxDOm0qgq9NUki1fqEhs9F60ikqgspS+oziS7IC8as8FeDS3tlQ0RAkA5OdDvhQRQ" +
                "PI75ULyHazTEm4Rak8TKmKl64pmnwcw4GS9fKWs7jRAuem1OtwA8HAqjaDeXC8Cd6fDfq7z5bZnE";
        // 原始数据
        String originalData = "{\"name\":\"tom\",\"age\":17,\"address\":\"shanghai\"}";

        // 以下两种方法都是可以加密解密的
        try {
            // 公钥加密，私钥解密
//            String encryptedByPublicKey = RSA.encryptByPublicKey(publicKeyString, originalData);
//            String decryptedByPrivateKey = RSA.decryptByPrivateKey(privateKeyString, encryptedByPublicKey);

            String encryptedByPublicKey = PersonalTool.rsaEncodeByPublicKey(publicKeyString, originalData);
            String decryptedByPrivateKey = PersonalTool.rsaDecodeByPrivateKey(privateKeyString, encryptedByPublicKey);

            System.out.println("公钥加密，私钥解密：");
            System.out.println("原始数据：" + originalData);
            System.out.println("公钥加密后：" + encryptedByPublicKey);
            System.out.println("私钥解密后：" + decryptedByPrivateKey);

            // 私钥加密，公钥解密
//            String encryptedByPrivateKey = RSA.encryptByPrivateKey(privateKeyString, originalData);
//            String decryptedByPublicKey = RSA.decryptByPublicKey(publicKeyString, encryptedByPrivateKey);

            String encryptedByPrivateKey = PersonalTool.rsaEncodeByPrivateKey(privateKeyString, originalData);
            String decryptedByPublicKey = PersonalTool.rsaDecodeByPublicKey(publicKeyString, encryptedByPrivateKey);
            System.out.println("\n私钥加密，公钥解密：");
            System.out.println("原始数据：" + originalData);
            System.out.println("私钥加密后：" + encryptedByPrivateKey);
            System.out.println("公钥解密后：" + decryptedByPublicKey);

        }catch (Exception e) {
            e.printStackTrace();
        }


    }

    //测试base64编码和解码
    public static void base64() {
        String content = "I am a student,I like go to school!";
        System.out.println(content);
        String encode = PersonalTool.base64encode(content);
        System.out.println(encode);
        String decode = PersonalTool.base64decode(encode);
        System.out.println(decode);
    }

    //打印json
    public static void json() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", "12");
        //json编码
        String json = PersonalTool.json_encode(map);
        System.out.println(json);
        //直接打印
        System.out.println(PersonalTool.json_decode(json));
        //分开打印
        for (Map.Entry<String, Object> entry : PersonalTool.json_decode(json).entrySet()) {
            System.out.println("    " + entry.getKey() + " : " + entry.getValue());
        }
    }

    //测试融360接口
    public static void rongAPI() {
        // 是否联调环境, 上线时替换
        boolean isTest = true;
        // 融360分配的appid, 上线时替换
        String appId = "5002021";
        // 融360分配的密钥, 注意不能删除回车和空格, 上线时替换
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANjRElfubbJKHnLeMXN6A0JeEmJVi4oVIFJ1u23sNMju5R4eCniHGRlHZxDrcvrAKiSx1zmBDaWwGunkgAuOEJPTAJv1hYOQz/lLbEIkclO2UQ8djLtITvE2zOpQmixqzd7FE5kY4y9zeShVPqgHOWMgrLKXsRpBc0p46fLi47rdAgMBAAECgYByvfpiNJkzIIyKp9It1aWB6vz5s2H8en+3g/zkCHxK1jrEvIs4C+8ZdLMzvhcEYswqzT8kfIq3Y8Jqg3XMJ6vXRfjl6R5u275sKFnH18shwtmCgmx5R+1yODvx9gd7sw6m9m7Z8ZBjt4E1n8r+1xF8Y8I0ojW9kaziOqbWtbjKtQJBAPL8Kpp1XNigHasS6ii2piC0pu6Lc/F0JZJ6Nfc8s0bs4bZIOiqn0N+pLJSSM/ga2bBFC0k5ZHJ1LzbwMC6a84cCQQDkbhUe8tViqsEsjd3OdvwvsLHvugQWNnIri1apFeSXPG7rKN9Owo2LWqyNkcfXhauNio5ZCsJDzZsQx6PVCb97AkEAn63PYYnW5hTy6kWX1AGT0tJlEUZbf0VQm53scPlW5zksa1mzv+xge1r1jP03YchjXk9BR3GL7RvNV1EomLgDaQJAYkjTIZvOIEPkFe18cCdV3pciGElrFLucaAvt35ah4kiIWPofCt4+1KTQ9aCx4ZOL+ryQ/tLYPyAqBhA/95vYHQJAaLOq+PEv0V5IpRneNFFFJdTukkNDOXPDEiy6XR+locaUPD+T6nxIgyv+jBn0fAQLOLHrv5Qh0rNwqHwbqZAEnQ==";
        //融360分配的总账号ID, 联调时替换
        String uid = "861098";
        // 实例化Rong360服务类
        Rong360Service service = new Rong360Service(appId, privateKey, isTest);

        // 获取订单接口
        Map<String, String> params = new HashMap<String, String>();
        params.put("uid", uid);
        params.put("begin_time", "2025-03-10 00:00:00");
        params.put("end_time", "2025-03-10 23:59:59");
        System.out.println(service.getorderlist(params));

    }

    //导出数据到excel文件
    public static void exportExcel() {
        String contentPath = PersonalTool.getUploadPath() + File.separator + "content.txt";
        String excelPath = PersonalTool.getUploadPath() + File.separator + "demo.xlsx";
        try {
            //读取数据并打印
            List<Map<String, String>> params = ExcelReader.readDataFromFile(contentPath);
            //控制台打印
            for (Map<String, String> param : params) {
                //打印的时候，这个是数组，所以需要使用Arrays.toString
                System.out.println(param);
            }
            String result = ExcelReader.exportExcel(contentPath, excelPath);
            System.out.println("导出的 Excel 文件路径: " + result);
        } catch (Exception e) {
            // 打印异常信息
            System.err.println("导出 Excel 文件时出现错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    //测试导入excel文件
    public static void importExcel() {
        String path = PersonalTool.getUploadPath() + File.separator + "demo.xlsx";
        try {
            //java使用File加载文件
            File file = new File(path);
            //调用工具读取excel表格
            List<Map<String, Object>> result = ExcelReader.readExcel(file);
            //直接打印内容
            for (Map<String, Object> row : result) {
                System.out.println(row);
            }
            // 将读取的数据写入到 content.txt 文件中，内容为追加，不删除 content.txt 的原始数据
            String txtFilePath = PersonalTool.getUploadPath() + File.separator + "content.txt";
            try (FileWriter writer = new FileWriter(txtFilePath, true)) {
                for (Map<String, Object> row : result) {
                    StringBuilder line = new StringBuilder();
                    for (Map.Entry<String, Object> entry : row.entrySet()) {
                        line.append(entry.getKey()).append(": ").append(entry.getValue()).append("\t");
                    }
                    // 去掉最后一个制表符
                    if (line.length() > 0) {
                        line.deleteCharAt(line.length() - 1);
                    }
                    line.append("\n");
                    writer.write(line.toString());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 在队列中执行带有多个参数的匿名函数
    public static void runWithManyParams() {
        // 模拟业务的带多个参数的回调函数
        MultiParamCallback<MultiParams> businessCallback = (params) -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("业务处理完成，传入的参数1是: " + params.getParam1() + "，参数2是: " + params.getParam2());
        };
        // 创建多个线程投递任务
        for (int i = 0; i < 5; i++) {
            // 填充多个参数
            final MultiParams params = new MultiParams("参数 " + i, i);
            // 在线程中奖参数投递到队列中
            new Thread(() -> {
                Map<String, Object> result = PersonalTool.runWithLineAndParams("businessKey", businessCallback, params);
                // 输出任务执行结果
                System.out.println(Thread.currentThread().getName() + " 业务操作结果: " + result.get("status"));
            }).start();
        }
    }

    //排队任务，并且带有参数
    public static void runLineWithParameter() {
        // 模拟业务的带参数回调函数
        ParametrizedCallback<String> businessCallback = (param) -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("业务处理完成，传入的参数是: " + param);
        };

        for (int i = 0; i < 5; i++) {
            final String param = "参数 " + i;
            new Thread(() -> {
                Map<String, Object> result = PersonalTool.runWithLine("businessKey", businessCallback, param);
                System.out.println(Thread.currentThread().getName() + " 业务操作结果: " + result.get("status"));
            }).start();
        }
    }

    //排队按顺序执行
    public static void runLine() {
        // 模拟业务的回调函数
        Runnable businessCallback = () -> {
            try {
                Thread.sleep(1000); // 模拟业务处理耗时
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("业务处理完成");
        };

        // 创建多个线程模拟多个请求
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                Map<String, Object> result = PersonalTool.runWithLine("businessKey", businessCallback);
                System.out.println(Thread.currentThread().getName() + " 业务操作结果: " + result.get("status"));
            }).start();
        }
    }

    // 测试互斥锁
    public static void lockDemo() {
        Runnable business2Callback = () -> {
            try {
                Thread.sleep(1000); // 模拟业务处理耗时
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("业务 2 处理完成");
        };
        // 创建多个线程模拟多个请求
        for (int i = 0; i < 5; i++) {
            //创建线程
            Thread thread = new Thread(() -> {
                boolean result = PersonalTool.getMutexByKey("mutex", business2Callback);
                System.out.println(Thread.currentThread().getName() + " 操作结果: " + (result ? "成功" : "失败"));
            });
            //启动线程
            thread.start();
        }
    }

    //测试http请求
    public static void httpTest() {
        String url = "http://my.crm.com/admin/info/filterInfo";
        Map<String, Object> data = new HashMap<>();
        data.put("phone", "7b751ba84854309c3bf7bd5f98aa417e");
        data.put("sourceCode", "yinbiaolianke");
        data.put("accessKey", "2dc0b42f-8763-417f-b3f3-637628fb58ca");

        // 发送 application/x-www-form-urlencoded 格式的 POST 请求
        Map<String, String> headersForm = new HashMap<>();
        headersForm.put("Content-Type", "application/x-www-form-urlencoded");
        Map<String, Object> resultForm = HttpClient.post(url, data, headersForm);
        System.out.println("Form POST result: " + resultForm);

        // 发送 application/json 格式的 POST 请求
        Map<String, String> headersJson = new HashMap<>();
        headersJson.put("Content-Type", "application/json");
        Map<String, Object> resultJson = HttpClient.post(url, data, headersJson);
        System.out.println("JSON POST result: " + resultJson);

        // 发送 GET 请求
        Map<String, Object> resultGet = HttpClient.get("http://www.baidu.com", data);
        System.out.println("GET result: " + resultGet);
    }

    //http 异步请求
    public static void httpTest2() {
        String postUrl = "http://my.crm.com/admin/info/filterInfo";
        Map<String, Object> postData = new HashMap<>();
        postData.put("phone", "7b751ba84854309c3bf7bd5f98aa417e");
        postData.put("sourceCode", "yinbiaolianke");
        postData.put("accessKey", "2dc0b42f-8763-417f-b3f3-637628fb58ca");
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");

        CompletableFuture<Map<String, Object>> postFuture = HttpClient.postAsync(postUrl, postData, headers);
        //这个result是异步任务返回的结果集，异步任务定义什么类型，这里的result就是什么类型
        postFuture.thenAccept(result -> {
            System.out.println("POST 异步请求结果: " + result);
            PersonalTool.log(200, "post异步任务执行完成", new HashMap<>());
        });

        String getUrl = "http://www.baidu.com";
        Map<String, Object> getData = new HashMap<>();
        getData.put("param1", "value1");
        getData.put("param2", "value2");

        CompletableFuture<Map<String, Object>> getFuture = HttpClient.getAsync(getUrl, getData);
        getFuture.thenAccept(result -> {
            System.out.println("GET 异步请求结果: " + result);
            PersonalTool.log(200, "get异步任务执行完成", new HashMap<>());
        });

        System.out.println("主线程继续执行其他任务...");

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //测试cbc加密
    public static void runCbc() {
        try {
            // 将你提供的字符串设置为变量 data , 这个json值结尾不可以有转义符 \
            String data = "{\"orderNumber\":\"123456789\",\"productName\":\"demo\",\"price\":500}";
            String key = "c7bc6f032be58963"; // 16 字节密钥
            String iv = "C235E0BEM01Z7B87"; // 16 字节向量

            // 加密
            String encryptedData = AesEncryptionUtil.encryptByAes(data, key, iv);
            System.out.println("Encrypted Data: " + encryptedData);

            // 解密
            java.util.Map<String, Object> decryptedData = AesEncryptionUtil.decryptByAes(encryptedData, key, iv);
            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //当需要测试的时候，可以在这个main方法里面测试需要运行的代码，这样子就不用编译了，因为编译实在太麻烦了
    public static void test() {
        //检测加密方法是否可用
        Map<String, Object> channelInfo = PersonalTool.getChannelInformation("chengyitong");
        System.out.println(channelInfo);
        Map<String, Object> data = new HashMap<>();
        data.put("phone", "13983913423");
        data.put("age", 21);
        data.put("city", "重庆市");
        data.put("loan", "5000");

        String key = channelInfo.get("key").toString();
        // 使用 Jackson 的 ObjectMapper 将 Map 转换为 JSON 字符串
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String json = objectMapper.writeValueAsString(data);
            // 加密
            String encode = PersonalTool.xiaoWeiEncrypt(json, key);
            // 组装返回值
            Map<String, Object> map = new HashMap<>();
            map.put("sourceCode", "chengyitong");
            map.put("accessKey", channelInfo.get("accessKey").toString());
            map.put("data", encode);
            System.out.println(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}